﻿using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml.Schema;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace ScormModel
{
    [Serializable()]
    public class Item
    {
        //47/222
        //metadata
        private string _itemType = "";
        private string _identifier;
        private string _isvisible;
        private string _identifierref;
        private string _parameters;
        private string _title;
        private Metadata _metaData;
        private string _timeLimitAction;
        private string _dataFromLMS;
        private CompletionThreshold _completionThreshold;
        
        private List<Item> _listChildItem;
        private Sequencing _sequencing;
        private Presentation _presentation;
        private DataInfo _dataInfo;
         [NonSerialized]
        private XmlNode _itemContent;
        [XmlIgnoreAttribute]
        public string ItemType
        {
            get { return this._itemType; }
            set { this._itemType = value; }
        }
        [XmlAttribute("identifier")]
        public string Identifier
        {
            get { return this._identifier; }
            set { this._identifier = value; }
        }
        [XmlAttribute("identifierref")]
        public string Identifierref
        {
            get { return this._identifierref; }
            set { this._identifierref = value; }
        }

        [XmlAttribute("isvisible")]
        public string Isvisible
        {
            get { return this._isvisible; }
            set { this._isvisible = value; }
        }

        [XmlAttribute("parameters")]
        public string Parameters
        {
            get { return this._parameters; }
            set { this._parameters = value; }
        }

        [XmlElement("title")]
        public string Title
        {
            get { return this._title; }
            set { this._title = value; }
        }
        [XmlElement("metadata")]
        public Metadata Metadata
        {
            get { return this._metaData; }
            set { this._metaData = value; }
        }
        [XmlElement("item")]
        public List<Item> ListChildItem
        {
            get { return this._listChildItem; }
            set { this._listChildItem = value; }
        }
        [XmlElement("timeLimitAction",Namespace = "http://www.adlnet.org/xsd/adlcp_v1p3")]
        public string TimeLimitAction
        {
            get { return this._timeLimitAction; }
            set { this._timeLimitAction = value; }
        }
        [XmlElement("dataFromLMS", Namespace = "http://www.adlnet.org/xsd/adlcp_v1p3")]
        public string dataFromLMS
        {
            get { return this._dataFromLMS; }
            set { this._dataFromLMS = value; }
        }
        [XmlElement("completionThreshold", Namespace = "http://www.adlnet.org/xsd/adlcp_v1p3")]
        public CompletionThreshold CompletionThreshold
        {
            get { return this._completionThreshold; }
            set { this._completionThreshold = value; }
        }
        [XmlElement("presentation", Namespace = "http://www.adlnet.org/xsd/adlnav_v1p3")]
        public Presentation Presentation
        {
            get { return this._presentation; }
            set { this._presentation = value; }
        }
        [XmlElement("sequencing", Namespace = "http://www.imsglobal.org/xsd/imsss")]
        public Sequencing Sequencing
        {
            get { return this._sequencing; }
            set { this._sequencing = value; }
        }
        [XmlElement("data", Namespace = "http://www.adlnet.org/xsd/adlcp_v1p3")]
        public DataInfo DataInfo
        {
            get { return this._dataInfo; }
            set { this._dataInfo = value; }
        }
       
        [XmlIgnoreAttribute]
        public XmlNode ItemContent
        {
            get { return this._itemContent; }
            set { this._itemContent = value; }
        }
        public void buildItem(string courseTemplate)
        { 
            //incomplete
            _listChildItem = new List<Item>();
            XmlAttributeCollection attList = _itemContent.Attributes;    //Lay danh sach thuoc tinh    
            XmlNodeList childList = _itemContent.ChildNodes; //Lay danh sach node con
            XmlElement element = (XmlElement)_itemContent;
            XmlNodeList title_list = element.GetElementsByTagName("title");
            _title = title_list[0].InnerText;
            #region Lay id
            for (int i = 0; i < attList.Count; i++)
            {
                //Get id
                if (attList[i].Name == "identifier")
                {
                    _identifier = attList[i].Value;
                    string type = ItemUtils.checkTypeOfItem(_identifier, courseTemplate);
                    _itemType = type;
                }
                //idfef
                else if (attList[i].Name == "identifierref")
                {
                    _identifierref = attList[i].Value;
                }
                else if (attList[i].Name == "isvisible")
                {
                    _isvisible = attList[i].Value;
                }
                else if (attList[i].Name == "parameters")
                {
                    _parameters = attList[i].Value;
                }
            }
            #endregion
            #region Lay child
            for (int i = 0; i < childList.Count; i++)
            {
                //Add Children Item                    
                if (childList[i].Name == "item")
                {
                    Item temp = new Item();
                    temp._itemContent = childList[i];
                    temp.buildItem(courseTemplate);
                    _listChildItem.Add(temp);

                }
                else if (childList[i].Name == "imsss:sequencing")
                {
                    _sequencing = new Sequencing();
                    _sequencing .SequencingContent = childList[i];
                    _sequencing.buildSequencing(courseTemplate);
                }
                else if (childList[i].Name == "adlnav:presentation")
                {
                    _presentation = new Presentation();
                    _presentation.PresentationContent = childList[i];
                    _presentation.buildPresentation();
                }
                else if (childList[i].Name == "adlcp:timeLimitAction")
                {
                    _timeLimitAction = childList[i].InnerText;
                }
                else if (childList[i].Name == "metadata")
                {
                    _metaData = new Metadata();
                    _metaData.MetadataContent = childList[i];
                    _metaData.buildMetadata();
                }
                else if (childList[i].Name == "adlcp:dataFromLMS")
                {
                    _dataFromLMS = childList[i].InnerText;
                    
                }
                else if (childList[i].Name == "adlcp:completionThreshold")
                {
                    _completionThreshold = new ScormModel.CompletionThreshold();
                    _completionThreshold.CompletionThresholdContent = childList[i];
                    _completionThreshold.buildCompletionThreshold();
                }
                else if (childList[i].Name == "adlcp:data")
                {
                    _dataInfo = new DataInfo();
                    _dataInfo.DataContent = childList[i];
                    _dataInfo.buildData();
                }

            }
            #endregion
        }
        public TreeViewItem createItemTreeNode()
        {
            TreeViewItem itemRootNode = TreeUtil.GetTreeView(_title, "TaskIcon.png");
            for (int i = 0; i < _listChildItem.Count; i++)
            {
                TreeViewItem itemNode = _listChildItem[i].createItemTreeNode();
                itemRootNode.Items.Add(itemNode);
            }
            return itemRootNode;
        }
    }
}
